//
//  TestTableViewCell.m
//  TableViewDemo
//
//  Created by jintao he on 2018/12/19.
//  Copyright © 2018 jintao he. All rights reserved.
//

#import "TestTableViewCell.h"
#import "YYText.h"
#import <CommonCrypto/CommonDigest.h>

#define SCREEN_WIDTH [[UIScreen mainScreen] bounds].size.width

#define TestTableViewCell_SELF __weak __typeof(self) weakSelf = self;

@interface TestTableViewCell ()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) YYLabel *contentLabel;
@property (nonatomic, strong) UIImageView *coverImg;

@end

@implementation TestTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = [UIColor whiteColor];
        
        [self.contentView addSubview:self.titleLabel];
        [self.contentView addSubview:self.contentLabel];
        [self.contentView addSubview:self.coverImg];
        
        self.contentView.userInteractionEnabled = YES;
        
        self.coverImg.frame = CGRectMake(10, 10, 120, 90);
        self.titleLabel.frame = CGRectMake(140, 10, SCREEN_WIDTH - 150, 20);
        self.contentLabel.frame = CGRectMake(140, 40, SCREEN_WIDTH - 150, 0);
        
    }
    return self;
}

-(void)setPostData:(ResponseModel *)postData{
    
    self.titleLabel.text = postData.Title.length > 0 ? postData.Title : @"";
    
    NSString *content = postData.Context.length > 0 ? postData.Context : @"";
    
    [self.contentLabel setTextVerticalAlignment:YYTextVerticalAlignmentCenter];
    self.contentLabel.userInteractionEnabled = YES;
    self.contentLabel.numberOfLines = 0;
    
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:content];
    
    @try {
        /*
         @property NSString* TargetString;
         @property NSString* Url;
         */
        [attr addAttribute:NSForegroundColorAttributeName value:[UIColor grayColor] range:NSMakeRange(0, content.length)];
        
        for (ClickInfo *info in postData.ClickInfoList) {
            @autoreleasepool {
                
                [attr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:[content rangeOfString:info.TargetString]];
                TestTableViewCell_SELF
                if (info.TargetString.length > 0) {
                    [attr yy_setTextHighlightRange:[content rangeOfString:info.TargetString] color:[UIColor redColor] backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
                        if (weakSelf.highLightTextTapBlock) {
                            weakSelf.highLightTextTapBlock(info.TargetString,info.Url);
                        }
                    }];
                }
            }
        }
        
    } @catch (NSException *exception) {
        
    } @finally {
        
    }
    
    self.contentLabel.attributedText = attr;
    
    CGFloat contentHeight = [TestTableViewCell heightForSummary:postData.Context];
    self.contentLabel.frame = CGRectMake(140, 40, SCREEN_WIDTH - 150, contentHeight);
    
    NSString *path_document = NSHomeDirectory();
    
    NSString *imagePath = imagePath = [path_document stringByAppendingString:[NSString stringWithFormat:@"/Documents/%@.png",[self getMd5String:postData.ImageUrl]]];
    
    if (imagePath.length > 0) {
        UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
        self.coverImg.image = image;
    }else{
        self.coverImg.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:postData.ImageUrl]]];
    }
}

-(NSString *)getMd5String:(NSString *)targetStr{
    const char *data = [targetStr UTF8String];
    
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    
    CC_MD5(data, (CC_LONG)strlen(data), digest);
    
    NSMutableString *result = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    
    for (int i = 0; i < CC_MD5_DIGEST_LENGTH; i ++) {
        @autoreleasepool {
            [result appendFormat:@"%02x", digest[i]];
        }
    }
    return result;
}

+(CGFloat)cellHeightForType:(ResponseModel *)pdata{
    CGFloat contentHeight = 50 + [self heightForSummary:pdata.Context];
    
    return contentHeight < 110 ? 110 : contentHeight;
}

/** 计算文字高度  */
+(CGFloat)heightForSummary:(NSString *)summary{
    if (summary.length == 0) {
        return 0;
    }
    
    NSDictionary *attr = @{NSFontAttributeName : [UIFont systemFontOfSize:13]
                           };
    
    CGRect rect = [summary boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 150, HUGE)
                                        options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                     attributes:attr context:nil];
    
    return ceil(rect.size.height)+1;
}

#pragma mark -- lazy

-(UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.font = [UIFont systemFontOfSize:15];
        _titleLabel.textColor = [UIColor blackColor];
    }
    return _titleLabel;
}

-(YYLabel *)contentLabel{
    if (!_contentLabel) {
        _contentLabel = [YYLabel new];
        _contentLabel.font = [UIFont systemFontOfSize:13];
        _contentLabel.textColor = [UIColor grayColor];
        _contentLabel.numberOfLines = 0;
    }
    return _contentLabel;
}

-(UIImageView *)coverImg{
    if (!_coverImg) {
        _coverImg = [[UIImageView alloc] init];
        _coverImg.backgroundColor = [UIColor cyanColor];
    }
    return _coverImg;
}

@end
